<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="98px">
      <el-form-item label="合同编码" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          placeholder="请输入合同编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同甲方商户号" prop="partyAId" label-width="120px">
        <el-input
          v-model="queryParams.partyAId"
          placeholder="请输入合同甲方商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同状态" prop="contractStatus">
        <el-select v-model="queryParams.contractStatus" placeholder="请选择合同状态" clearable
                   size="small">
          <el-option
            v-for="dict in contractStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签约日期" prop="signDate">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sign:contractSign:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sign:contractSign:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sign:contractSign:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="contractSignList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="合同编码" align="center" prop="contractId"/>
      <el-table-column label="合同甲方商户号" align="center" prop="partyAId"/>
      <el-table-column label="合同乙方商户号" align="center" prop="partyBId"/>
      <el-table-column label="合同状态" align="center" prop="contractStatus"
                       :formatter="contractStatusFormat">
        <template slot-scope="scope">
          <span v-if="scope.row.contractStatus === '1'"><el-tag type="success" effect="dark"
                                                                size="mini">有效</el-tag></span>
          <span v-if="scope.row.contractStatus === '2'"><el-tag type="danger" effect="dark"
                                                                size="mini">无效</el-tag></span>
        </template>
      </el-table-column>

      <el-table-column label="签约日期" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewUpdate(scope.row,'view')"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewUpdate(scope.row,'edit')"
            v-hasPermi="['sign:contractSign:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sign:contractSign:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台签约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="合同编码" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同编码"  :disabled="isEdit"/>
        </el-form-item>
        <el-form-item label="合同甲方商户号" prop="partyAId">
          <el-input v-model="form.partyAId" placeholder="请输入合同甲方商户号"/>
        </el-form-item>
        <el-form-item label="合同乙方商户号" prop="partyBId">
          <el-input v-model="form.partyBId" placeholder="请输入合同乙方商户号"/>
        </el-form-item>
        <el-form-item label="合同状态" prop="contractStatus">
          <el-select v-model="form.contractStatus" placeholder="请选择合同状态">
            <el-option
              v-for="dict in contractStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签约日期" prop="signDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.signDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择签约日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title !== '查看平台签约'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listContractSign,
    getContractSign,
    delContractSign,
    addContractSign,
    updateContractSign,
    exportContractSign
  } from "@/api/sign/contractSign";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 日期范围
        dateRange: [],
        // 平台签约表格数据
        contractSignList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否是编辑状态
        isEdit: true,
        // 合同状态字典
        contractStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contractId: undefined,
          partyAId: undefined,
          contractStatus: undefined,
          signDate: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          contractId: [
            {required: true, message: "合同编码不能为空", trigger: "blur"}
          ], partyAId: [
            {required: true, message: "合同甲方商户号不能为空", trigger: "blur"}
          ], partyBId: [
            {required: true, message: "合同乙方商户号不能为空", trigger: "blur"}
          ], contractStatus: [
            {required: true, message: "合同状态不能为空", trigger: "blur"}
          ], signDate: [
            {required: true, message: "签约日期不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("contract_status").then(response => {
        this.contractStatusOptions = response.data;
      });
    },
    methods: {
      /** 查询平台签约列表 */
      getList() {
        this.loading = true;
        listContractSign(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.contractSignList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 合同状态字典翻译
      contractStatusFormat(row, column) {
        return this.selectDictLabel(this.contractStatusOptions, row.contractStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          contractId: undefined,
          partyAId: undefined,
          partyBId: undefined,
          contractStatus: undefined,
          signDate: undefined,
          corpCode: undefined,
          corpName: undefined,
          createBy: undefined,
          createDate: undefined,
          reqIp: undefined,
          status: "0",
          updateBy: undefined,
          updateDate: undefined,
          remarks: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.isEdit = false;
        this.open = true;
        this.title = "添加平台签约";
      },
      /** 修改按钮操作 */
      handleViewUpdate(row, type) {
        this.reset();
        const id = row.id || this.ids
        getContractSign(id).then(response => {
          this.form = response.data;
          this.open = true;
          if ('edit' === type) {
            this.isEdit = true;
            this.title = "修改平台签约";
          }
          if ('view' === type) {
            this.isEdit = false;
            this.title = "查看平台签约";
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateContractSign(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addContractSign(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delContractSign(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出平台签约数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportContractSign(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
